package top.blackr.folivora.common.security.auth.impl;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.springframework.stereotype.Service;
import top.blackr.folivora.common.core.constants.CommonConstants;
import top.blackr.folivora.common.security.auth.LoginAuthentication;
import top.blackr.folivora.common.security.auth.SecUser;
import top.blackr.folivora.common.security.crud.model.vo.SysUserVo;
import top.blackr.folivora.common.security.crud.repository.SysUserRepository;

@Slf4j
@AllArgsConstructor
@Service(CommonConstants.LoginType.SMS)
public final class SmsLoginServiceImpl extends DefaultLoginService {

    private final SysUserRepository sysUserRepository;

    @Override
    public SecUser authentication(LoginAuthentication authentication) {
        String phone = String.valueOf(authentication.getPrincipal());
        String code = String.valueOf(authentication.getCredential());
        // todo 9999通用测试验证码
        if (!StrUtil.equals("9999", code)) {
            return null;
        }
        // 获取用户
        SysUserVo userInfo = sysUserRepository.loginForSms(phone);
        return BeanUtil.copy(userInfo, SecUser.class);
    }
}
